using System;
using System.Collections.Generic;
using System.Text;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Microsoft.SharePoint.WebControls;

namespace Aras.WebParts
{
    public class CustomPager : SPGridViewPager
    {

        protected override void Render(System.Web.UI.HtmlTextWriter output)
        {
            if ((this.GridViewControl != null)
                    && (this.PreviousPageLinkIsEnabled || this.NextPageLinkIsEnabled))
            {
                // Not done: could be customized based on the this.GridViewControl.PagerStyle settings ... 
                output.Write("<table cellspacing='0' border='0' style='width:100%;'><tr><td align='center' valign='middle' style='padding-top:0px;padding-bottom:4px;vertical-align:middle;font-weight:bold;color=DarkBlue;background-color:#f2f2f2;border-color:LightGrey;border-width:1px;border-style:solid;'>");

                this.RenderImage(output, this.PreviousPageLinkIsEnabled, "previouspage", "/_layouts/images/prev.gif", "/_layouts/images/blank.gif", "Previous Page");
                output.Write(string.Format("Page {0} of {1}", (this.GridViewControl.PageIndex+1).ToString(), this.GridViewControl.PageCount.ToString()));
                this.RenderImage(output, this.NextPageLinkIsEnabled, "nextpage", "/_layouts/images/next.gif", "/_layouts/images/blank.gif", "Next Page");

                output.Write("</td></tr></table>");
            }
        }

        private bool PreviousPageLinkIsEnabled
        {
            get
            {
                if (this.GridViewControl != null)
                {
                    if (this.GridViewControl.PageIndex > 0)
                    {
                        return true;
                    }
                }
                return false;
            }
        }

        private bool NextPageLinkIsEnabled
        {
            get
            {
                if (this.GridViewControl != null)
                {
                    if (this.GridViewControl.PageIndex < (this.GridViewControl.PageCount - 1))
                    {
                        return true;
                    }
                }
                return false;
            }
        }

        private void RenderImage(HtmlTextWriter output, bool isEnabled, string postbackEventArgument, string enabledImageUrl, string disabledImageUrl, string toolTip)
        {
            string str = disabledImageUrl;
            if (isEnabled)
            {
                output.Write("<a href=\"javascript:");
                output.Write(this.Page.ClientScript.GetPostBackEventReference(this, postbackEventArgument));
                output.Write("\">");
                str = enabledImageUrl;
            }
            output.Write("<img src=\"");
            output.Write(str);
            output.Write("\" border=\"0\" alt=\"");
            output.Write(toolTip);
            output.Write("\"/>");
            if (isEnabled)
            {
                output.Write("</a>");
            }
        }
    }
}
